import modelExtend from 'dva-model-extend';
import { pageModel } from '@/common/model/BaseModel';
import {listParameters, getParameters, saveParameters, delParameters, editParameters, getCode} from '../services/ParametersService';

export default modelExtend(pageModel, {
  namespace: 'parameters',
  state: {
    currentUser: {},
    orgData: [],
    currentItem: {},
    modalType: '',
    selectedRowKeys: [],
    formValues: {},
  },
  effects: {
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(getParameters, payload);
      yield put({
        type: 'saveCurrentParameters',
        payload: response.data,
      });
    },

    // 右侧按条件查询
    *fetchParameters({ payload }, { call, put }) {
      const response = yield call(listParameters, payload);
      yield put({
        type: 'updateState',
        payload: {
          list: response.data.records,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: response.data.total,
            current: response.data.current,
          },
        },
      });
    },
    // 编辑按钮
    *edit({ payload }, { call, put }) {
      const response = yield call(getParameters, payload);
      if (response && response.data) {
        yield put({
          type: 'updateState',
          payload: {
            modalType: 'edit',
            currentItem: response.data,
          },
        });
      }
    },

    // 保存提交
    *save({ payload }, { call, put }) {
      const response = yield call(saveParameters, payload);
      if (response && response.data) {
        yield put({
          type: 'updateState',
          payload: {
            modalType: '',
            currentItem: {},
            list: response.data.records,
            pagination: {
              showSizeChanger: false,
              showQuickJumper: true,
              showTotal: total => `共 ${total} 条`,
              total: response.data.total,
              current: response.data.current,
            },
          },
        });
      } else {
        yield put({
          type: 'updateState',
          payload: {
            modalType: '',
            currentItem: {},
          },
        });
      }
    },

    // 查询
    *listParameters({ payload }, { call, put }) {
      // 查询数据
      const response = yield call(listParameters, payload);
      yield put({
        type: 'updateState',
        payload: {
          list: response.data.records,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: response.data.total,
            current: response.data.current,
          },
        },
      });
    },

    // 删除
    *remove({ payload }, { call, put }) {
      const response = yield call(delParameters, payload);
      if (response && response.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: response.data.records,
            pagination: {
              showSizeChanger: false,
              showQuickJumper: true,
              showTotal: total => `共 ${total} 条`,
              total: response.data.total,
              current: response.data.current,
            },
            selectedRowKeys: [],
          },
        });
      }
    },

    // 更改可用状态
    *changeStatus({ payload }, { call, put }) {
      const response = yield call(editParameters, payload);
      if (response && response.success) {
        const oldPayload = payload;

        oldPayload.record.status = payload.status;
        yield put({
          type: 'updateState',
          payload: {
            data: response.data,
            selectedRowKeys: [],
          },
        });
      }
    },

    // 获取code
    *getCode({ payload }, { call, put }) {
      const response = yield call(getCode, payload);
      yield put({
        type: 'updateState',
        payload: {
          modalType: 'create',
          currentItem: response.data,
        },
      });
    },
  },
});
